﻿using Microsoft.Extensions.Options;
using MongoDB.Driver;
using SubStationManagement.Server.Models;

namespace SubStationManagement.Server.Services
{
    public class DIDOSampleService
    {
        private readonly IMongoCollection<DIDOSample> _dIDOSamplesCollection;

        public DIDOSampleService(
            IOptions<SubStationDatabaseSettings> subStationDatabaseSettings)
        {
            var mongoClient = new MongoClient(
                subStationDatabaseSettings.Value.ConnectionString);

            var mongoDatabase = mongoClient.GetDatabase(
                subStationDatabaseSettings.Value.DatabaseName);

            _dIDOSamplesCollection = mongoDatabase.GetCollection<DIDOSample>(
                subStationDatabaseSettings.Value.DIDOSamplesCollectionName);
        }

        public async Task<List<DIDOSample>> GetAsync() =>
            await _dIDOSamplesCollection.Find(_ => true).ToListAsync();

        public List<DIDOSample> GetAll() =>
            _dIDOSamplesCollection.Find(_ => true).ToList();

        public async Task<DIDOSample?> GetAsync(string id) =>
            await _dIDOSamplesCollection.Find(x => x.Id == id).FirstOrDefaultAsync();

        public async Task CreateAsync(DIDOSample newDIDOSample) =>
            await _dIDOSamplesCollection.InsertOneAsync(newDIDOSample);

        public async Task UpdateAsync(string id, DIDOSample updatedDIDOSample) =>
            await _dIDOSamplesCollection.ReplaceOneAsync(x => x.Id == id, updatedDIDOSample);

        public async Task RemoveAsync(string id) =>
            await _dIDOSamplesCollection.DeleteOneAsync(x => x.Id == id);
    }
}
